package com.enfi.api.configuration;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

/**
 * @author wind
 * @date 2020/4/9
 */
@Configuration
public class SysConfiguration {
    @Value("${spring.dozer.basename:}")
    private String dozerBaseName;

    @Bean
    public DozerBeanMapper mapper() {
        DozerBeanMapper mapper = new DozerBeanMapper();
        mapper.setMappingFiles(Arrays.asList(dozerBaseName));
        return mapper;
    }

    @Bean("jackson2ObjectMapperBuilderCustomizer")
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        Jackson2ObjectMapperBuilderCustomizer customizer = new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
                jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance)
                        .serializerByType(Long.TYPE, ToStringSerializer.instance);
            }
        };
        return customizer;
    }

    @ConditionalOnBean(CorsConfig.class)
    @Bean
    public WebMvcConfigurer corsConfigurer(ObjectProvider<CorsConfig> corsConfigObjectProvider) {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                CorsConfig corsConfig = corsConfigObjectProvider.getIfAvailable();
                if (corsConfig != null) {
                    if (corsConfig.getDetails() != null) {
                        corsConfig.getDetails().forEach(v ->
                                registry.addMapping(v.getPath())
                                        .allowedOrigins(v.getAllowedOrigins())
                                        .allowedHeaders((v.getAllowedHeaders()))
                                        .exposedHeaders(v.getExposedHeaders())
                                        .allowedMethods(v.getAllowedMethods())
                                        .allowCredentials(v.getAllowCredentials())
                        );
                    }

                }

            }
        };
    }
}
